#!/usr/bin/env python
        self.tn.write(username.encode() + b'\n')
        rely = self.tn.expect([], timeout=1)[2].decode().strip()    #读显
        if verbose:
            print(rely)
#coding:utf-8

'导入模块'
from telnetlib import Telnet
import time
import re
import logging

'定义类'
class TelnetClient():
    '初始化属性'
    def __init__(self):
        self.tn = Telnet()
    '定义login_host函数，用于登陆设备'
    def login_host(self,ip,password,enable=None,verbose=True):
        '连接设备，try-except结构'
        try:
            self.tn.open(ip,port=23)
        except:
            logging.warning('%s网络连接失败' %ip)
            return False
        '''
        '输入用户名'
        self.tn.read_until(b'Username:', timeout=1)
        self.tn.write(b'\n')
         '''
        '输入用户密码'

        self.tn.read_until(b'Password:', timeout=1)
        self.tn.write(password.encode() + b'\n')
        rely = self.tn.expect([], timeout=1)[2].decode().strip()
        if verbose:
            print(rely)
        '进去特权模式'
        if enable is not None:
            self.tn.write(b'enable\n')
            self.tn.write(enable.encode() + b'\n')
            if verbose:
                rely = self.tn.expect([], timeout=1)[2].decode().strip()
                print(rely)
                time.sleep(1)

        rely = self.tn.read_very_eager().decode()
        if 'Login invalid' not in rely:
            logging.warning('%s登陆成功' % ip)
            return True
        else:
            logging.warning('%s登陆失败，用户名或密码错误' % ip)
            return False

    '定义do_cmd函数,用于执行命令'
    def do_cmd(self,cmds):
        confComplete = ''
        '读取文件，for语句循环执行命令'
        with open(cmds) as cmd_obj:
            for cmd in cmd_obj:
                self.tn.write(cmd.encode().strip() + b'\n')
                time.sleep(6)
                rely = self.tn.read_very_eager().decode()
                time.sleep(1)
                print(rely)
                confComplete = confComplete + rely
                #logging.warning('命令执行结果:\n %s' %rely)

        #判断配置文件是否加载完毕
        moreReg = '--More--'
        moreFlag = re.search(moreReg,confComplete)
        while(moreFlag):
            self.tn.write(' '.encode())
            time.sleep(1)
            rely = self.tn.read_very_eager().decode()
            #confComplete = re.sub(moreReg,' ',confComplete)
            confComplete = confComplete + rely
            moreFlag = re.search(moreReg,rely)
            print(rely)
            time.sleep(1)

        #保存配置
        #print(confComplete)
        nameReg = 'hostname.+[0-9]'
        name = re.search(nameReg,confComplete)
        name = str(name)
        name = name.split('hostname ')
        name = name[1].split("'")
        name = name[0]

   #    print("hello")
    #   print(ip.strip())
        print(name)

        filename = name+'('+ip.strip()+')'+'.txt'
      # print(filename)
        file1 = open(filename,'a+')
        confComplete = re.sub(moreReg,'...',confComplete)
        file1.write(confComplete)
        file1.close()


    '定义logout_host函数，关闭程序'
    def logout_host(self):
        self.tn.close()

if __name__ == '__main__':
    username = 'cisco'  #用户名
    password = 'cisco' #密码
    enable = 'cisco'    #特权密码
    lists = 'list.txt'  #存放IP地址文件，相对路径
    cmds = 'cmd.txt'    #存放执行命令文件，相对路径
    telnet_client = TelnetClient()
    '读取文件，for语句循环登陆IP'
    with open(lists,'rt') as list_obj:
        for ip in list_obj:
            '如果登录结果为True，则执行命令，然后退出'
            if telnet_client.login_host(ip.strip(),username,password,enable):
                time.sleep(1)
                telnet_client.do_cmd(cmds)
                time.sleep(2)
                telnet_client.logout_host()
                time.sleep(2)
